package com.haixiaoke.business;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.util.StringUtil;
import com.haixiaoke.common.annotation.RepeatSubmit;
import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.domain.*;
import com.haixiaoke.common.oss.service.OSSService;
import com.haixiaoke.service.*;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKEventHandleService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Dell
 * @Date 2023/9/26
 **/
@RestController
@RequestMapping("/smart/lock/hxk")
public class SmartLockHxkController extends BaseController {

    @Autowired
    private ISmartLockService smartLockService;

    @Autowired
    private OSSService ossService;

    @Autowired
    private ISmartLockCardService iSmartLockCardService;
    @Autowired
    private ISmartLockPwdService iSmartLockPwdService;
    @Autowired
    private ISmartLockFaceService iSmartLockFaceService;
    @Autowired
    private ILockYltAccreditService iLockYltAccreditService;

    @Autowired
    private SmartLockHXKEventHandleService smartLockHXKEventHandleService;

    @GetMapping("/list")
    @ResponseBody
    public String listByUserId(SmartLock smartLock) {
        return smartLockHXKEventHandleService.selectSmartLockList(smartLock);
    }

    @GetMapping("/getTaskList")
    @ResponseBody
    public String getTaskList(SmartLock smartLock) {
        return smartLockHXKEventHandleService.selectTaskList(smartLock);
    }

    /**
     * 添加用户
     * @param smartLock
     * @return
     */
    @PostMapping("/addUser")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addUser(/*@RequestParam("file") MultipartFile file,@RequestParam*/ @RequestBody SmartLock smartLock){
        /*if (file != null && !file.isEmpty() && file.getSize() >= 0) {
           String faceUrl = ossService.putFile(null, file, "ad");
           smartLock.setFaceImage(faceUrl);
        }*/
        if (StringUtil.isEmpty(smartLock.getIdentification())) {
            return error("身份标识不能为空");
        }
        SmartLock lock = smartLockService.selectBySn(smartLock.getSn());
        if (StrUtil.isNotEmpty(smartLock.getPassword())) {
            SmartLockPwd smartLockPwd = iSmartLockPwdService.selectSmartLockPwdBySNAndIdentification(smartLock.getSn(),smartLock.getIdentification());
            if (ObjectUtil.isNotEmpty(smartLockPwd)) {
                return error("身份标识已被使用");
            }
        }
        if (StrUtil.isNotEmpty(smartLock.getCardNumber())) {
            SmartLockCard smartLockCard = iSmartLockCardService.selectSmartLockPwdBySNAndIdentificationAndCardType(smartLock.getSn(),smartLock.getIdentification(),smartLock.getCardType());
            if (ObjectUtil.isNotEmpty(smartLockCard)) {
                return error("身份标识已被使用");
            }
        }
        if (StrUtil.isNotEmpty(smartLock.getFaceImage())) {
            SmartLockFace smartLockFace = iSmartLockFaceService.selectSmartLockPwdBySNAndIdentification(smartLock.getSn(),smartLock.getIdentification());
            if (ObjectUtil.isNotEmpty(smartLockFace)) {
                return error("身份标识已被使用");
            }
        }
        if (ObjectUtil.isEmpty(lock) && StrUtil.isNotEmpty(smartLock.getSn())) {
            smartLock.setBrandName("嗨小克");
            smartLock.setModelId((int) SmartLock.FIVE_GENERATION_SMART_LOCK);
            smartLock.setModelName("HXK");
            smartLock.setBindingStatus(1);
            smartLock.setVoiceStatus(0);
            smartLock.setBrandId( SmartLock.FIVE_GENERATION_SMART_LOCK);
            smartLockService.insertSmartLock(smartLock);
        }
        return toAjax(smartLockHXKEventHandleService.insertSmartLockAll(smartLock));
    }

    @PostMapping("/offline/password")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult offlinePwd(@RequestBody SmartLock smartLock){
        return smartLockHXKEventHandleService.offlinePwd(smartLock);
    }

    /**
     * 远程开锁
     * @param smartLock
     * @return
     */
    @GetMapping("/remoteUnlock")
    @ResponseBody
    public AjaxResult remoteUnlock(SmartLock smartLock){
        return toAjax(smartLockHXKEventHandleService.remoteUnlock(smartLock));
    }

    /**
     * 密钥设置
     * @param smartLock
     * @return
     */
    @GetMapping("/secretKey")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult setSecretKey(SmartLock smartLock){
        if (smartLock.getSecretKey().length()>11 || smartLock.getSecretKey().length() <6) {
            return error("密钥过长!");
        }
        return toAjax(smartLockHXKEventHandleService.setSecretKey(smartLock));
    }

    /**
     * 恢复出厂设置
     * @param smartLock
     * @return
     */
    @GetMapping("/lockReset")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult lockReset(SmartLock smartLock){
        return toAjax(smartLockHXKEventHandleService.lockReset(smartLock));
    }

    @GetMapping("/lockOTA")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult lockOTA(SmartLock smartLock){
        if (StrUtil.isEmpty(smartLock.getSn())) {
            return AjaxResult.error("设备sn缺失");
        }
        return toAjax(smartLockHXKEventHandleService.lockOTA(smartLock));
    }

    /**
     * 删除指定用户
     * @param smartLock
     * @return
     */
    @GetMapping("/removeUser")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult removeUser(SmartLock smartLock){
        return toAjax(smartLockHXKEventHandleService.removeUser(smartLock));
    }

    /**
     * 删除全部用户
     * @param smartLock
     * @return
     */
    @GetMapping("/removeUserAll")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult removeUserAll(SmartLock smartLock){
        smartLock.setDeleteUserFlag(true);
        return toAjax(smartLockHXKEventHandleService.removeUserAll(smartLock));
    }

    /**
     * 删除全部普通用户
     * @param smartLock
     * @return
     */
    @GetMapping("/removeUserPortion")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult removeUserPortion(SmartLock smartLock){
        smartLock.setDeleteUserFlag(false);
        return toAjax(smartLockHXKEventHandleService.removeUserAll(smartLock));
    }

}
